<?php
namespace Controllers;

use Models\User;

class UserController extends CommonController {

    public function login() {
        // $_POST接收的数据是从表单传过来 form标签
        $request = json_decode(file_get_contents('php://input'), true);

        $username = isset($request['username']) ? trim($request['username']) : '';
        if (empty($username)) return $this->response(400, '用户名必须填写');

        $password = isset($request['password']) ? $request['password'] : '';
        if (empty($password)) return $this->response(400, '密码必须填写');

        $userModel = new User();
        $user = $userModel->getUserByUsername($username);
        if (empty($user)) return $this->response(400, '用户名不存在');
        if ($user['password'] != md5($password)) return $this->response(400, '密码错误');
        return $this->response(200, '登陆成功');
    }

}